from django.contrib.sites.models import Site
from settings import SLUG_HASH_KEY, ORDER_HASH_KEY
from studenthoods.apps.shop.models import Shop, Order
import hashlib

def generate_hash(value):
    hash = hashlib.sha1()
    hash.update(value)
    hash = hash.hexdigest()
    return str(hash)

def generate_shop_slug_hash(slug):
    return generate_hash(slug + SLUG_HASH_KEY)

def authenticate_shop_slug_hash(auth_token, email):
    if auth_token == generate_auth_token(email):
        return True
    else:
        return False
    
def get_shop_slug_hash(slug_hash):
    try:
        shop = Shop.objects.get(slug_hash=slug_hash)
    except:
        shop =[]
    return shop

def generate_order_hash(datetime, email, shop, size, colour):
    return generate_hash(datetime + email + shop + size + colour+ ORDER_HASH_KEY)

def get_order_hash(hash):
    try:
        order = Order.objects.get(hash = hash)
    except Order.DoesNotExist:
        order =[]
    return order

def set_order_status(hash, status):
    order = get_order_hash(hash)
    
    if order:
        order.status = status
        order.save()

def get_site_url(url):
    return 'http://%s%s' % (Site.objects.get_current().domain, str(url))